package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.pojo.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HomeController {

    /**
     * 处理根路径访问
     * @param session HTTP会话
     * @return 如果用户已登录返回主页，否则重定向到登录页面
     */
    @GetMapping("/")
    public ModelAndView home(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        
        // 检查用户是否已登录
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser == null) {
            // 用户未登录，重定向到登录页面
            modelAndView.setViewName("redirect:/t/login");
        } else {
            // 用户已登录，显示主页
            modelAndView.setViewName("index");
            modelAndView.addObject("user", currentUser);
        }
        
        return modelAndView;
    }
}